# -*- coding: utf-8 -*-
import subprocess
import httplib2
import urllib
import json
import bs4

def convert_str(s, from_code, to_code):
    try:
        return s.decode(from_code, 'ignore').encode(to_code)
    except:
        pass

    return ''

def __fetch_html(url):
    try:
        h = httplib2.Http(".cache")  
        resp, content = h.request(url, "GET")  
        return resp, content
    except:
        pass

    return None, ''

def fetch_html(url):
    try:
        resp, content = __fetch_html(url)
        if content != None and content != '':
            return bs4.BeautifulSoup(content)
    except:
        pass

    return None
    
def fetch_js(url, referer = ''):
    try:
        args = ['./tp/phantomjs', './tp/url.js', url]
        if referer != '':
            args.append(referer)

        p = subprocess.Popen(args, bufsize=8192, stdout=subprocess.PIPE).stdout
        content = ''
        if p:
            for line in p:
                content += line
            p.close()

        if content != '':
            return bs4.BeautifulSoup(content)

    except Exception, e:
        pass

    return None
    
def fetch_json(url, encoding = 'utf-8'):
    try:
        p = subprocess.Popen(['./tp/phantomjs', './tp/json.js', url], bufsize=8192, stdout=subprocess.PIPE).stdout
        content = ''
        if p:
            for line in p:
                content += line
            p.close()

        if content != '':
            o = json.loads(content)
            return o
    except Exception, e:
        pass

    return None

def fetch_raw(url):
    try:
        resp, content = __fetch_html(url)
        if content != None and content != '':
            return content
    except:
        pass

    return None
    
def fetch_raw_json(url):
    try:
        resp, content = __fetch_html(url)
        if content != None and content != '':
            return json.loads(content.strip())
    except:
        pass

    return None
    
def fetch_page(url, type = 'html', referer = ''):
    if type == 'html':
        return fetch_html(url)
    elif type == 'js':
        return fetch_js(url, referer)
    elif type == 'json':
        return fetch_json(url)
    elif type == 'raw':
        return fetch_raw(url)
    elif type == 'raw_json':
        return fetch_raw_json(url)

    return None

def __sub_str_begin(s, token):
    pos = s.find(token)
    if pos < 0:
        return ''
    return s[pos + len(token):]

def __sub_str_end(s, token):
    pos = s.find(token)
    if pos < 0:
        return ''

    return s[:pos]

def sub_str(s, *tokens):
    count = len(tokens)
    if count < 2:
        return ''

    s1 = s
    cur = 0
    for token in tokens:
        if cur < count - 1:
            s1 = __sub_str_begin(s1, token)
        else:
            s1 = __sub_str_end(s1, token)

        if s1 == '':
            break

        cur += 1

    return s1
